import logo from './logo.svg';
import './App.css';
import './web-components/HelloWorld.js'
import './web-components/SayHello.js'
import './web-components/UserCard.js'
import {useState, useEffect, useRef} from 'react'

function App() {
  const [username] = useState('Lam')
  const [info] = useState({name: 'Lam', age: 18})
  const userCardRef = useRef()

  useEffect(()=>{
    const handler = (e)=>{
      alert(e.detail.message)
    }

    const domRef = userCardRef.current
    if(domRef){
      domRef.addEventListener('myevent', handler)
    }

    return () => {
      if(domRef)
        domRef.removeEventListener('myevent', handler)
    }
  }, [])

  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
        <wc-hello-world></wc-hello-world>
        <wc-say-hello username={username}></wc-say-hello>
        <wc-user-card ref={userCardRef} info={JSON.stringify(info)}></wc-user-card>
      </header>
    </div>
  );
}

export default App;
